<?php
##############################################
#
#   菜单配置
# @ Time：2016-12-9 - 上午12:00:09
# @ Author ：JiangYongWei <1085562810@qq.com>
# @ Encoding ：UTF-8 language ：PHP
#
##############################################

namespace Admin\Model;

class MenuModel extends AdminBaseModel{
	/* 用户组表  */
	protected $tableName = 'auth_group';	
	
	protected $_db;
	
	public function __construct($tableName=''){
		//初始化数据库对象
		$this -> _db = M();
		if(!empty($tableName)){
			$this -> tableName = $tableName;
		}	
		parent::__construct($this -> tableName);
	}
	
	/**
	 * 获取菜单
	 */
	public function getMenuData(){
		$data = $this -> _db -> query("select * from mt_admin_nav where pid = 0 AND is_display = 1 order by order_number");
		
		foreach ($data as $k => $v){
			$data[$k]['child'] = $this -> _db -> query("select * from mt_admin_nav where pid = {$v['id']} AND is_display = 1 order by order_number");
		}
		return $data;
	}
	
	/**
	 * 	添加一级菜单
	 * @param array $data
	 */
	public function addOneLevelMenu($data){
		$sql = "insert into mt_admin_nav values(default, {$data['p_id']}, '{$data['name']}', 
												'{$data['mca']}', '{$data['ico']}', {$data['order_number']},1)";
		return $this -> _db -> execute($sql);
	}
	
	/**
	 * 修改菜单
	 * @param array $data
	 */
	public function editMenu($data){
		$sql = "update mt_admin_nav set name = '{$data['name']}', mca = '{$data['mca']}', ico = '{$data['ico']}' 
				where id = {$data['id']}";
		return $this -> _db -> execute($sql);
	}
	
	/**
	 * 删除菜单
	 * @param int $id
	 */
	public function deleteMenu($id){
		//判断是否有父级菜单存在
		$data = $this -> _db -> query("select id from mt_admin_nav where pid = $id");
		
		if(count($data) > 0){
			return 1;
		}else{
			$result = $this -> _db -> execute("delete from mt_admin_nav where id = $id");
			if($result)
				return 2;
			else 
				return 3;
		}
	}
	
}